<?php

///////
function print_header()
{
	print '<fb:dashboard>';
	print '<fb:create-button href="index.php?action=new">Create a new question list!</fb:create-button>';
	print '</fb:dashboard>';
	
	// style
	$mystyle = htmlentities(file_get_contents('dn_css.css', true));
	print '<style>' .$mystyle. '</style>';
}


function print_section_start($title)
{	
	$fbml = 
	'<div class="dn_subhead dn_mgnTop dn_mgnBottom dn_clearfix">
		<div class="left">';

	print $fbml . $title . '</div>';
	print '</div>';	
}

function print_section_end()
{
	
}


function debug_print($ret)
{
	print '<p>';
	print_r($ret);
	print '</p>';
}

//
// TODO: make the ask page AJAX-able
//
function exp_print_unanswered_q($uid)
{
	$q_list = get_all_unanswered_questions($uid);
	
	//debug_print($q_list);
	print_section_start('NEW! You got new questions.');

	print '<table>';	
	foreach($q_list as $q) {
		$reviewer = $q['asked_by'];
		print '<tr>';
		print '<td valign="top"><fb:profile-pic style="padding: 4px" uid="'.$reviewer.'"/><br><fb:name uid="'.$reviewer.'" />';
      	print '</td>';
		
		// get all answer box IDs
		$a_box = '';
		for($j = 0; $j < $q['howmany_q']; $j++){
			$a_box = 'History'.$q['s_id'].$j.', '.$a_box;
		}
		
		print '<td>';
		
		//
		// input <fb:editor> style
		//
		print '<span style="display:none"><fb:editor></fb:editor></span>';
						
		print '<form action="?tab=ask&action=save">';
		print '<ul style="list-style: none; margin: 0px; padding: 0">';
		print '<li style="padding: 2px 0px"><fb:name uid="' .$reviewer. '" firstnameonly="true" linked="true"/>\'s fun questions to you:';
		print '<a id="show_text'.$q['s_id'].'" href="#" clicktohide="show_text'.$q['s_id'].'" clicktoshow="' .$a_box. ',hide_text'.$q['s_id'].'">answer them</a>';
		print '<a id="hide_text'.$q['s_id'].'" href="#" clicktohide="' .$a_box. ', hide_text'.$q['s_id'].'" clicktoshow="show_text'.$q['s_id'].'" style="display:none">hide</a>';
		print '</li>';
		$i = 0;
		while(true) {
			if('' == $q[$i]) {
				break;
			} else {
				print '<li style="padding: 2px 0px">'.$q[$i]['q_content']. '</li>';
				print '<li>[contributed by <fb:name uid="' .$q[$i]['creator']. '"/>] ';
  				// print '<div id="History'.$q['asked_by'].$i.'" style="display:none">THE STUFF TO DISPLAY</div>';  			  				
  				//print '<div id="History'.$q['asked_by'].$i.'" style="display:none"><fb:editor-textarea label="answer' .$q[$i]['q_id']. '" name="answer' .$q[$i]['q_id']. '"/></div>';
				print '</li>';

				print '<li>';				
 				print '<textarea id="History'.$q['s_id'].$i.'" name="profiletext" cols="60" rows="3" style="display:none">'; 
  				print 'WRITTEN AUTO-TEXT.'; 
  				print '</textarea><br>';
  				print '</li>';


				$i++;	
			}
		}
		print '</ul>';
		
		print '<fb:editor-buttonset>';  
		print '<fb:editor-button value="Submit" name="submit"/>';
		print '<fb:editor-button value="Ignore" name="ignore"/>';
		print '</fb:editor-buttonset>';
		print '</form>';

		
		print '</td>';		
		print '</tr>';
	}
   	print '</table>';
	print_section_end();
}


function print_unanswered_q($uid)
{
	$sheet_list = get_all_unanswered_questions($uid);
	
	//debug_print($q_list);
	print_section_start('NEW! You got new questions.');

	print '<table>';	
	foreach($sheet_list as $sheet) {
		$reviewer = $sheet['asked_by'];
		print '<tr>';
		print '<td valign="top"><fb:profile-pic style="padding: 4px" uid="'.$reviewer.'"/><br><fb:name uid="'.$reviewer.'" />';
      	print '</td>';
		
		// get all answer box IDs
		$a_box = '';
		for($j = 0; $j < $sheet['howmany_q']; $j++){
			$a_box = 'History'.$sheet['s_id'].$j.', '.$a_box;
		}
		
		print '<td>';
		print '<fb:editor action="?tab=ask&action=save">';
		
		// so hidden input for pass parameters
		print '<input type="hidden" name="s_id" value="'. $sheet['s_id'] .'"/>';
		print '<input type="hidden" name="q_list" value="'. $sheet['q_list'] .'"/>';
		
		print '<ul style="list-style: none; margin: 0px; padding: 0">';
		print '<li style="padding: 2px 0px"><fb:name uid="' .$reviewer. '" firstnameonly="true" linked="true"/>\'s fun questions to you:';
		print '<a id="show_text'.$sheet['s_id'].'" href="#" clicktohide="show_text'.$sheet['s_id'].'" clicktoshow="' .$a_box. ',hide_text'.$sheet['s_id'].'">answer them</a>';
		print '<a id="hide_text'.$sheet['s_id'].'" href="#" clicktohide="' .$a_box. ', hide_text'.$sheet['s_id'].'" clicktoshow="show_text'.$sheet['s_id'].'" style="display:none">hide</a>';
		print '</li>';
		$i = 0;
		while(true) {
			if('' == $sheet[$i]) {
				break;
			} else {
				$q = $sheet[$i];
				print '<li style="padding: 2px 0px">'.$q['q_content']. '</li>';
				print '<li>[contributed by <fb:name uid="' .$q['creator']. '"/>] ';
				print '</li>';

				print '<li>';				
 				print '<textarea id="History'.$sheet['s_id'].$i.'" name="a_content'.$q['q_id'].'" cols="60" rows="3" style="display:none">'; 
  				print 'WRITTEN AUTO-TEXT.'; 
  				print '</textarea>';
  				print '</li>';


				$i++;	
			}
		}
		print '</ul>';
		
		print '<fb:editor-buttonset>';  
		print '<fb:editor-button value="Submit" name="submit"/>';
		print '<fb:editor-button value="Ignore" name="ignore"/>';
		print '</fb:editor-buttonset>';
		print '</fb:editor>';

		
		print '</td>';		
		print '</tr>';
	}
   	print '</table>';
	print_section_end();
}


function print_tabs($selected)
{
	global $APP_ROOT_URL;
	
	$arr_title = array(	
						0 => "Ask & Asked", 
						1 => "Between Others", 
						2 => "Invite");
						
	$arr_tabname = array(
						0 => "ask", 
						1 => "others", 
						2 => "invite");

	$arr_selected = array(						
						0 => "false", 
						1 => "false", 
						2 => "false");

	$arr_selected[$selected] = "true";
	
	$tab_number = count($arr_title);
	
	print '<fb:tabs>';
		for($i = 0 ; $i < $tab_number; $i++) {
			$my_title = $arr_title[$i];
			$my_tab_url = $APP_ROOT_URL . '/index.php?tab=' . $arr_tabname[$i];
			$my_selected = $arr_selected[$i];
			
			print '<fb:tab_item href="' .$my_tab_url. '" title="' .$my_title. '" selected="' .$my_selected. '" />';
		}
	print '</fb:tabs>';
}

/////

function start_page()
{
  print '<div style="padding: 0px 10px 10px">';
}

function close_page()
{
  print '</div>';
}

function pic($url, $size)
{
  if (!preg_match('/^(.*)[a-z]([0-9_]+.jpg)$/', $url, $parts))
    return 'error';
  return $parts[1] . $size . $parts[2];
}

function print_restaurant($r)
{
  global $APP_ROOT_URL;
  print '<div style="width: 200px; float: left; height: 140px">';
  print '<a href="'.$APP_ROOT_URL.'/index.php?action=view&restaurant='.$r['restaurant'].'"><img src="'.pic($r['pic'], 's').'" border=0></a>';
  print '<h4 style="padding-top: 3px"><a href="'.$APP_ROOT_URL.'/index.php?action=view&restaurant='.$r['restaurant'].'">';
  print $r['name'].'</a></h4>';

  print '</div>';
}

function print_ajax_rater($page, $user, $restaurant_id, $current_rating)
{
  print '<form id="foo"><div id="vote"> <h3>Add a Rating:</h3><br/>
  <a href="#" clickrewriteid="vote" clickrewriteform="foo" clickrewriteurl="'.$page.'?user='.$user.'&restaurant_id='.$restaurant_id.'&rating=1"> Poor</a><br/>
  <a href="#" clickrewriteid="vote" clickrewriteform="foo" clickrewriteurl="'.$page.'?user='.$user.'&restaurant_id='.$restaurant_id.'&rating=2"> Fair</a><br/>
  <a href="#" clickrewriteid="vote" clickrewriteform="foo" clickrewriteurl="'.$page.'?user='.$user.'&restaurant_id='.$restaurant_id.'&rating=3"> Good</a><br/>
  <a href="#" clickrewriteid="vote" clickrewriteform="foo" clickrewriteurl="'.$page.'?user='.$user.'&restaurant_id='.$restaurant_id.'&rating=4"> Quite Good</a><br/>
  <a href="#" clickrewriteid="vote" clickrewriteform="foo" clickrewriteurl="'.$page.'?user='.$user.'&restaurant_id='.$restaurant_id.'&rating=5"> Awesometown</a><br/>';



  if ($current_rating !== 0)
  {
    print '<br/>';
    print'<a href="#" clickrewriteid="vote" clickrewriteform="foo" clickrewriteurl="'.$page.'?user='.$user.'&restaurant_id='.$restaurant_id.'&rating=0"> REMOVE MY RATING</a><br/>';
  }

  print '</div>
  </form>';

}

function print_nonajax_rater($user, $restaurant_id, $current_rating)
{
  global $APP_ROOT_URL;
  print '<h3>Add a rating:</h3><br/>';
  print '<a href="'.$APP_ROOT_URL.'/index.php?action=rate&restaurant='.$restaurant_id.'&rating=1"> Poor</a><br/>';
  print '<a href="'.$APP_ROOT_URL.'/index.php?action=rate&restaurant='.$restaurant_id.'&rating=2"> Fair</a><br/>';
  print '<a href="'.$APP_ROOT_URL.'/index.php?action=rate&restaurant='.$restaurant_id.'&rating=3"> Good</a><br/>';
  print '<a href="'.$APP_ROOT_URL.'/index.php?action=rate&restaurant='.$restaurant_id.'&rating=4"> Quite Good</a><br/>';
  print '<a href="'.$APP_ROOT_URL.'/index.php?action=rate&restaurant='.$restaurant_id.'&rating=5"> Awesometown</a><br/>';
  if ($current_rating !== 0)
  {
    print '<br/>';
    print '<a href="'.$APP_ROOT_URL.'/index.php?action=rate&restaurant='.$restaurant_id.'&rating=0"> REMOVE MY RATING</a><br/>';
  }

}

?>

